0274. H 指数【中等】
1. 📝 题目描述
给你一个整数数组 citations,其中 citations[i] 表示研究者的第 i 篇论文被引用的次数。计算并返回该研究者的 h 指数。
根据维基百科上 h 指数的定义:h 代表“高引用次数”,一名科研人员的 h 指数 是指他(她)至少发表了 h 篇论文,并且 至少 有 h 篇论文被引用次数大于等于 h。如果 h 有多种可能的值,h 指数 是其中最大的那个。
示例 1:
txt
输入:citations = [3,0,6,1,5]
输出:3
解释:
给定数组表示研究者总共有 5 篇论文,每篇论文相应的被引用了 3, 0, 6, 1, 5 次。
由于研究者有 3 篇论文每篇 至少 被引用了 3 次,其余两篇论文每篇被引用 不多于 3 次,所以她的 h 指数是 3。1
2
3
4
5
6
2
3
4
5
6
示例 2:
txt
输入:citations = [1,3,1]
输出:11
2
2
提示:
n == citations.length1 <= n <= 50000 <= citations[i] <= 1000
2. 🎯 s.1 - 计数排序
c
int hIndex(int* citations, int citationsSize) {
int n = citationsSize;
int* count = (int*)calloc(n + 1, sizeof(int));
for (int i = 0; i < n; i++) {
count[citations[i] < n ? citations[i] : n]++;
}
int total = 0;
for (int i = n; i >= 0; i--) {
total += count[i];
if (total >= i) { free(count); return i; }
}
free(count);
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
js
/**
* @param {number[]} citations
* @return {number}
*/
var hIndex = function (citations) {
const n = citations.length
const count = new Array(n + 1).fill(0)
for (const c of citations) {
count[Math.min(c, n)]++
}
let total = 0
for (let i = n; i >= 0; i--) {
total += count[i]
if (total >= i) return i
}
return 0
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
py
class Solution:
def hIndex(self, citations: List[int]) -> int:
n = len(citations)
count = [0] * (n + 1)
for c in citations:
count[min(c, n)] += 1
total = 0
for i in range(n, -1, -1):
total += count[i]
if total >= i:
return i
return 01
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
- 时间复杂度:
,其中 是数组长度 - 空间复杂度:
,计数数组
算法思路:
- 用计数数组统计引用次数,超过
的引用次数归入第 个桶 - 从后向前累加,找到第一个满足累计论文数
的 即为 H 指数